package com.eastfair.venueservice.service.impl;

import com.eastfair.common.api.TransactionOrderFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.dto.TransactionOrderDTO;
import com.eastfair.venueservice.service.VenueManageTransactionOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 交易订单 service.
 *
 * @author dqq
 * @date 2023 /3/23
 */
@Service
@Slf4j
public class VenueManageTransactionOrderServiceImpl implements VenueManageTransactionOrderService {

    @Resource
    private TransactionOrderFeign transactionOrderFeign;

    @Override
    public boolean save(TransactionOrderDTO transactionOrderDTO) {
        log.info("save - 保存更新交易记录, transactionOrderDTO={}", transactionOrderDTO);
        R<Boolean> result = transactionOrderFeign.save(transactionOrderDTO);
        log.info("save - 保存更新交易记录完成, result={}", result);
        if (!result.getIsSuccess()) {
            // 收款单位不能为空
//            if (VenueServiceExceptionCode.PAYEE_NOT_EXIST.getCode() == result.getCode()) {
//                log.error("save - 支付系统收款单位不能为空, code={}, result={}", result.getCode(), result.getMsg());
//                return false;
//            }
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean batchSave(List<TransactionOrderDTO> transactionOrderList) {
        log.info("batchSave - 批量新增交易订单, transactionOrderList={}", transactionOrderList);
        R<Boolean> result = transactionOrderFeign.batchSave(transactionOrderList);
        log.info("save - 批量新增交易订单, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean update(TransactionOrderDTO transactionOrderDTO) {
        log.info("update - 更新交易订单, transactionOrderDTO={}", transactionOrderDTO);
        R<Boolean> result = transactionOrderFeign.update(transactionOrderDTO);
        log.info("update - 更新交易订单完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean cancel(String orderNum) {
        log.info("cancel - 取消交易订单, orderNum={}", orderNum);
        R<Boolean> result = transactionOrderFeign.cancel(orderNum);
        log.info("update - 取消交易订单完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }
}